// 引入加密库
import CryptoJS from 'crypto-js'

/**
 * 加密函数
 * @param {*} word 需要加密字符
 * @param {*} key 密钥
 */
export const Encrypt = (word, key) => {
  let srcs = CryptoJS.enc.Utf8.parse(word)
  let srcKey = CryptoJS.enc.Utf8.parse(key)

  let encrypted = CryptoJS.DES.encrypt(srcs, srcKey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.ciphertext.toString(CryptoJS.enc.Base64)
}

/**
 * 解密函数
 * @param {*} word 需要加密字符
 * @param {*} key 密钥
 */
export const Decrypt = (word, key) => {
  let srcKey = CryptoJS.enc.Utf8.parse(key)
  let srcs = CryptoJS.enc.Base64.parse(word)

  let decrypt = CryptoJS.DES.decrypt({
    ciphertext: srcs
  }, srcKey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  return decryptedStr
}

/**
 * md5加密方式
 * @param {*} word 加密字符
 */
export const MD5crypt = word => {
  return CryptoJS.MD5(word).toString()
}
